package DAO;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import Beans.Courier;
import Utils.HibernateUtil;

public class CourierDao {

	Transaction tr = null;
	private SessionFactory sessionFactory;

	public CourierDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	/**
	 * 检查字段重复
	 * 
	 * @return
	 */
	public List<Courier> checkValue(String flag, String value) {
		Session session = sessionFactory.openSession();

		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Courier.class);
		if ("mobileNum".equals(flag)) {
			c.add(Restrictions.eq("mobileNum", new BigInteger(value)));
		}
		if ("rfid".equals(flag)) {
			c.add(Restrictions.eq("rfid", value));
		}
		if ("IDcard".equals(flag)) {
			c.add(Restrictions.eq("IDcard", value));
		}
		c.add(Restrictions.ne("status", 1));
		List<Courier> couriers = c.list();
		tr.commit();

		session.close();
		return couriers;
	}

	/**
	 * 通过快递员ID返回指定快递员信息
	 * 
	 * @return
	 */
	public Courier getCourierById(int courierId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Courier courier = (Courier) session.get(Courier.class, courierId);
		tr.commit();
		session.close();
		return courier;
	}

	/**
	 * 删除快递员
	 */
	public void delete(Courier courier) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.delete(courier);
		tr.commit();
		session.close();
	}

	/**
	 * 修改快递员
	 */
	public void edit(Courier courier) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(courier);
		tr.commit();
		session.close();
	}

	/**
	 * 添加快递员
	 * 
	 * @param courier
	 */
	public void save(Courier courier) {
		Session session = sessionFactory.openSession();

		tr = session.beginTransaction();
		session.save(courier);
		tr.commit();
		session.clear();
		session.close();
	}

	/**
	 * 获取所有快递员
	 * 
	 * @return
	 */
	public List<Courier> getAll() {
		Session session = sessionFactory.openSession();

		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Courier.class);
		c.add(Restrictions.ne("status", 1));
		List<Courier> couriers = c.list();
		tr.commit();

		session.close();
		return couriers;
	}

	/**
	 * 根据手机号码查询快递员
	 * 
	 * @param mobileNum
	 * @return
	 */
	public List<Courier> getByCourierPhone(BigInteger mobileNum) {
		Session session = sessionFactory.openSession();

		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Courier.class);
		c.add(Restrictions.eq("mobileNum", mobileNum));
		c.add(Restrictions.ne("status", 1));
		List<Courier> couriers = c.list();
		tr.commit();

		session.close();
		return couriers;
	}

	/**
	 * 根据rfid查询快递员列表
	 * 
	 * @param rfid
	 * @return
	 */
	public List<Courier> getByRfid(String rfid) {
		Session session = sessionFactory.openSession();

		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Courier.class);
		c.add(Restrictions.eq("rfid", rfid));
		c.add(Restrictions.ne("status", 1));
		List<Courier> couriers = c.list();
		tr.commit();

		session.close();
		return couriers;
	}

	/**
	 * 根据快递员姓名获取快递员列表
	 * 
	 * @param name
	 * @return
	 */
	public List<Courier> getByName(String name) {
		Session session = sessionFactory.openSession();

		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Courier.class);
		c.add(Restrictions.like("courierName", "%" + name + "%"));
		c.add(Restrictions.ne("status", 1));
		List<Courier> couriers = c.list();
		tr.commit();

		session.close();
		return couriers;
	}

	/**
	 * 判断快递员是否存在
	 * 
	 * @return
	 */
	public List<Courier> isCourierExit(String rfid) {
		Session session = sessionFactory.openSession();

		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Courier.class);
		c.add(Restrictions.eq("rfid", rfid));
		List<Courier> couriers = c.list();
		tr.commit();

		session.close();
		return couriers;
	}

	/**
	 * 获取某个快递员（list）
	 * 
	 * @return
	 */
	public List<Courier> getPackageCourier(int courierID) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Courier.class);
		c.add(Restrictions.eq("courierId", courierID));
		List<Courier> couriers = c.list();
		tr.commit();
		session.close();
		return couriers;
	}

	/**
	 * 根据公司和快递网点查询快递员
	 * 
	 * @param company
	 * @param branch
	 * @return
	 */
	public List<Courier> searchCourier(int expressId) {
		Session session = sessionFactory.openSession();

		ExpressDao expressBranchDao = new ExpressDao();

		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Courier.class);
		c.add(Restrictions.eq("express",
				expressBranchDao.getExpressBranch(expressId)));
		c.add(Restrictions.ne("status", 1));
		List<Courier> couriers = c.list();
		tr.commit();

		session.close();
		return couriers;
	}

	/**
	 * 根据手机号码获取快递员
	 * 
	 * @param mobileNum
	 * @return
	 */
	public Courier getByMobileNum(BigInteger mobileNum) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Courier.class);
		c.add(Restrictions.eq("mobileNum", mobileNum));
		List<Courier> couriers = c.list();
		tr.commit();
		session.close();
		if (couriers.size() > 0) {
			return couriers.get(0);
		} else {
			return null;
		}
	}

	public Courier getByCourierRfid(String rfid) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Courier.class);
		c.add(Restrictions.eq("rfid", rfid));
		List<Courier> couriers = c.list();
		tr.commit();
		session.close();
		if (couriers.size() > 0) {
			return couriers.get(0);
		} else {
			return null;
		}
	}

}
